package com.yunhe.abnormal.repository.data;

import com.yunhe.abnormal.domain.data.AbnormalSmsConfig;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface AbnormalSmsConfigRepository extends JpaRepository<AbnormalSmsConfig, Long> {

    Page<AbnormalSmsConfig> findAll(Specification<AbnormalSmsConfig> spec, Pageable pageable);

    Iterable<AbnormalSmsConfig> findAll(Specification<AbnormalSmsConfig> spec, Sort sort);

    Iterable<AbnormalSmsConfig> findAll(Specification<AbnormalSmsConfig> spec);

    AbnormalSmsConfig findByPhone(String phone);

    AbnormalSmsConfig findByUserId(@Param("userId") Long userId);

    @Query(value = "select * from data_config_abnormal a where a.user_id=:userId and a.is_remind=1", nativeQuery = true)
    AbnormalSmsConfig findByUserIdNeedInform(@Param("userId") Long userId);

    /**
     * 根据国区和手机号查找通知配置
     * @param phone
     * @return
     */
    @Query(value = "select * from data_config_abnormal a where a.international_code=:iCode and a.phone=:phone limit 1", nativeQuery = true)
    AbnormalSmsConfig findByICodeAndPhone(@Param("iCode") String iCode, @Param("phone") String phone);
}
